package com.xuecheng.teaching.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xuecheng.teaching.model.dto.WorkRecordDTO;
import com.xuecheng.teaching.model.dto.WorkRecordStatsDTO;
import com.xuecheng.teaching.model.entity.WorkRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface WorkRecordMapper extends BaseMapper<WorkRecord> {
    
    /**
     * 查询课程作业提交记录
     * @param coursePubId 课程发布ID
     * @return 作业提交记录列表
     */
    @Select("SELECT wr.*, w.question, tp.pname as teachplan_name " +
            "FROM work_record wr " +
            "LEFT JOIN work w ON wr.work_id = w.work_id " +
            "LEFT JOIN teachplan tp ON wr.teachplan_id = tp.teachplan_id " +
            "WHERE wr.course_pub_id = #{coursePubId} " +
            "ORDER BY wr.create_date DESC")
    List<WorkRecord> selectByCoursePubId(@Param("coursePubId") Long coursePubId);
    
    /**
     * 查询待批阅的作业数量
     * @param coursePubId 课程发布ID
     * @return 待批阅数量
     */
    @Select("SELECT COUNT(*) FROM work_record " +
            "WHERE course_pub_id = #{coursePubId} " +
            "AND status = '306001'")
    Integer selectToBeReviewedCount(@Param("coursePubId") Long coursePubId);



    /**
     * 按课程统计作业记录
     */
    List<Map<String, Object>> selectWorkRecordStatsByCourse(
            @Param("courseName") String courseName,
            @Param("offset") int offset,
            @Param("limit") int limit
    );

    /**
     * 统计课程数量
     */
    long countWorkRecordStatsByCourse(@Param("courseName") String courseName);

    /**
     * 查询作业记录详情
     */
    WorkRecordDTO selectWorkRecordDetail(@Param("workRecordId") Long workRecordId);



    /**
     * 统计课程作业相关数据
     * @param courseWorkId 课程作业ID
     * @return 统计结果
     */
    WorkRecordStatsDTO selectWorkRecordStats(@Param("courseWorkId") Long courseWorkId);
    /**
     * 查询课程计划下的作业记录
     * @param teachplanId 课程计划ID
     * @return 作业记录列表
     */
    List<WorkRecordDTO> selectByTeachplanId(@Param("teachplanId") Long teachplanId);
}